package cn.dyp.com.manualcontrol.dialog;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DialogFragment;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

import cn.dyp.com.manualcontrol.ClientApp;
import cn.dyp.com.manualcontrol.R;

/**
 * Ip设置类 显示和设置Ip地址
 * 
 * @author asus
 */

public class IpSetDialog extends DialogFragment {
	private EditText mIp1ET;
	private EditText mIp2ET;
	private EditText mIp3ET;
	private EditText mIp4ET;
	private Button mCloseBtn;
	private Button mOkBtn;
	private ClientApp mApp;
	private Activity mActivity;

	/**
	 * 构造函数
	 * 
	 * @param activity
	 *            Activity
	 */
	public IpSetDialog(Activity activity) {
		this.mApp = (ClientApp) activity.getApplication();
		this.mActivity = activity;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		getDialog().getWindow().setBackgroundDrawable(
				new ColorDrawable(android.graphics.Color.TRANSPARENT));
		getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
		return inflater.inflate(R.layout.set_ip_dialog, container, false);
	}

	@Override
	public void onActivityCreated(Bundle arg0) {
		super.onActivityCreated(arg0);
		initView();
		setListener();
		setValue();
	}

	/**
	 * 显示Ip地址
	 */
	private void setValue() {
		String ipStr[] = mApp.getServerIpStr().split("\\.");
		if (ipStr.length >= 4) {
			mIp1ET.setText(ipStr[0]);
			mIp2ET.setText(ipStr[1]);
			mIp3ET.setText(ipStr[2]);
			mIp4ET.setText(ipStr[3]);
		}
	}

	/**
	 * 设置监听器
	 * 
	 */
	private void setListener() {
		mCloseBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				IpSetDialog.this.dismiss();
			}
		});
		mOkBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setIp();
				IpSetDialog.this.dismiss();
			}
		});
	}

	/**
	 * 初始化控件
	 */
	private void initView() {
		mIp1ET = (EditText) getView().findViewById(R.id.ip1_edit_text);
		mIp2ET = (EditText) getView().findViewById(R.id.ip2_edit_text);
		mIp3ET = (EditText) getView().findViewById(R.id.ip3_edit_text);
		mIp4ET = (EditText) getView().findViewById(R.id.ip4_edit_text);
		mCloseBtn = (Button) getView().findViewById(R.id.close);
		mOkBtn = (Button) getView().findViewById(R.id.ok_button);
	}

	/**
	 * 显示对话框
	 * 
	 * @param title
	 *            对话框标题
	 * @param message
	 *            对话框内容
	 */
	private void showAlertDialog(String title, String message) {
		AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
		builder.setTitle(title);
		builder.setMessage(message);
		builder.setPositiveButton(mActivity.getString(android.R.string.ok),
				null);
		builder.show();
	}

	/**
	 * 对用户输入的Ip进行转化、校验和保存
	 * 
	 */
	private void setIp() {
		try {
			// 获取Ip地址
			int ip1, ip2, ip3, ip4;
			ip1 = Integer.parseInt(mIp1ET.getText().toString().trim());
			ip2 = Integer.parseInt(mIp2ET.getText().toString().trim());
			ip3 = Integer.parseInt(mIp3ET.getText().toString().trim());
			ip4 = Integer.parseInt(mIp4ET.getText().toString().trim());

			// 校验Ip地址
			String ipStr = ip1 + "." + ip2 + "." + ip3 + "." + ip4;
			if (null == ipStr || "".equals(ipStr)) {
				showAlertDialog(getString(R.string.prompt),
						getString(R.string.invalid_ip));
				return;
			} else {
				Pattern p = Pattern
						.compile("(2[5][0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})");
				Matcher m = p.matcher(ipStr);
				if (!m.matches()) {
					showAlertDialog(getString(R.string.prompt),
							getString(R.string.invalid_ip));
					return;
				}
			}

			mApp.setServerIpStr(ipStr);
		} catch (Exception e) {
			showAlertDialog(getString(R.string.prompt),
					getString(R.string.invalid_ip));
		}
	}
}
